package com.onehash.onebilling.gui.panel;

import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;



import com.onehash.onebilling.gui.*;
import com.onehash.onebilling.entity.*;
import com.onehash.onebilling.entity.User.*;

public class EditRemoveUserUI extends JPanel implements Observer{
    private UserRegister userRegister;

    JLabel imageLabel = new JLabel();
    JLabel Lheader = new JLabel();
    JTextField tfUserName = new JTextField();
    JLabel lUserName = new JLabel();
    
    JButton bEdit = new JButton();
    JButton bRemove = new JButton();
    JButton bView = new JButton();
    JButton bReset = new JButton();
    TitledBorder titledBorder1 = new TitledBorder("");
    
    boolean roleModified = false;
    //Reference to owner frame
    private JDialog owner;

    public EditRemoveUserUI() {
        try {
            userRegister = UserRegister.getInstance();
            /* Add UI to observer */
            userRegister.addObserver(this);
            
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public void setOwner(JDialog owner){
        this.owner=owner;
    }

    private void jbInit() throws Exception {
        this.setLayout(null);
        this.setMinimumSize(new Dimension(800,570));
        this.setPreferredSize(new Dimension(800, 570));
        imageLabel.setMaximumSize(new Dimension(800, 570));
        imageLabel.setMinimumSize(new Dimension(800, 570));
        imageLabel.setPreferredSize(new Dimension(800, 570));
        imageLabel.setBounds(new Rectangle(0, 0, 800, 570));
        Lheader.setFont(new java.awt.Font("Dialog", Font.BOLD, 18));
        Lheader.setForeground(Color.orange);
        Lheader.setText(":: Upgrade/Edit User ::");
        Lheader.setBounds(new Rectangle(485, 15, 300, 21));
        lUserName.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        lUserName.setText("User Name:");
        lUserName.setBounds(new Rectangle(107, 279, 81, 14));
        tfUserName.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        tfUserName.setBounds(new Rectangle(209, 194, 286, 18));
        tfUserName.addKeyListener(new EditRemoveUserUI_tfUserName_keyAdapter(this));
        
        bView.setBounds(new Rectangle(493, 423, 84, 25));
        bView.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        bView.setBorder(BorderFactory.createLineBorder(Color.black));
        bView.setActionCommand("view");
        bView.setText("View");
        bView.addActionListener(new EditRemoveUserUI_bView_actionAdapter(this));
       
        bEdit.setBackground(Color.white);
        bEdit.setBounds(new Rectangle(592, 461, 84, 25));
        bEdit.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        bEdit.setBorder(BorderFactory.createLineBorder(Color.black));
        bEdit.setActionCommand("update");
        bEdit.setText("Update");
        bEdit.setEnabled(false);
        bEdit.addActionListener(new EditRemoveUserUI_bEdit_actionAdapter(this));
        
        bRemove.setBounds(new Rectangle(591, 490, 84, 25));
        bRemove.setEnabled(false);
        bRemove.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        bRemove.setBorder(BorderFactory.createLineBorder(Color.black));
        bRemove.setActionCommand("remove");
        bRemove.setText("Remove");
        bRemove.addActionListener(new EditRemoveUserUI_bRemove_actionAdapter(this));
        
        bReset.setBounds(new Rectangle(591, 520, 84, 25));
        bReset.setEnabled(false);
        bReset.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        bReset.setBorder(BorderFactory.createLineBorder(Color.black));
        bReset.setActionCommand("resetPassword");
        bReset.setText("Reset Password");
        bReset.addActionListener(new EditRemoveUserUI_bReset_actionAdapter(this));
        
        this.add(Lheader);
        this.add(lUserName);
        this.add(tfUserName);
        this.add(bRemove);
        this.add(bReset);
        this.add(bView);
        this.add(bEdit);
        this.add(imageLabel);
        imageLabel.setIcon(new ImageIcon(GenericDialog.class.getResource("images/BgTransaction.jpg")));
    }

    /* Observer pattern. Populate the textfields with user's particular */
    public void update() {
        User tempUser;
        
        String planName;

        tempUser = userRegister.getSelectedUser();
        this.tfUserName.setText(tempUser.getUserName());
        //this.tfContact.setText(tempUser.getRole().toString());

        bEdit.setEnabled(true);
        tfUserName.setEnabled(false);
        bView.setEnabled(false);
        bReset.setEnabled(true);
        bRemove.setEnabled(true);

        roleModified = false;
    }

    //Observer methods
    public void showErrorMsg(String msg){
        MessageDialog.showErrorDialog((Frame)owner.getOwner(), owner.getTitle(), msg);
    }

    public void showInfoMsg(String msg){
        MessageDialog.showInfoDialog((Frame)owner.getOwner(), owner.getTitle(), msg);
    }
    //////////////////////////////////////////////


    /* Method to display dialog for feedback */
   public void displayInfoDialog( int success ){
       if( success == 1 ){
           MessageDialog.showInfoDialog( (Frame)owner.getOwner(), owner.getTitle(), "User: "+tfUserName.getText()+" Successfully Updated!" );
       }
       else if( success == -1 ){
           MessageDialog.showErrorDialog( (Frame)owner.getOwner(), owner.getTitle(), "User NOT allow to Change!" );
       }
       else if( success == 0 ){
           MessageDialog.showErrorDialog( (Frame)owner.getOwner(), owner.getTitle(), "User Not Found!" );
       }
   }

   /* Method to check for empty inputs */
   private boolean isUserNameFieldEmpty(){
       if( tfUserName.getText().compareTo("")==0 )
           return true;
       else
           return false;
   }

   /* Method to check for empty inputs */
    private boolean emptyFields(){
      

        return false;
    }

    /* Method to clear the text input fields */
    private void clearFields(){
       
        roleModified = false;
     }

    /* View Button Clicked */
    public void bView_actionPerformed(ActionEvent e) {
        if( isUserNameFieldEmpty() )
            MessageDialog.showInfoDialog( (Frame)owner.getOwner(), owner.getTitle(), "Please enter a user name." );
        else{
            if (!userRegister.displayUser(tfUserName.getText())) { //user not found
                displayInfoDialog(0);
                clearFields();
            }
        }
    }
    
    /* Remove Button Clicked */
    public void bRemove_actionPerformed(ActionEvent e) {
        if( isUserNameFieldEmpty() )
            MessageDialog.showInfoDialog( (Frame)owner.getOwner(), owner.getTitle(), "Please enter a user name." );
        else{
            if (!userRegister.displayUser(tfUserName.getText())) { //user not found
                displayInfoDialog(0);
                clearFields();
            }
        }
    }
    
    public void bReset_actionPerformed(ActionEvent e) {
    	/*
        bEdit.setEnabled(false);
        bView.setEnabled(true);
        bRemove.setEnabled(false);
        tfUserName.setEnabled(true);
        clearFields();
        */
    }

    /* Edit Button clicked */
    public void bEdit_actionPerformed(ActionEvent e) {
    	
//        String plan;
//
//        /* Checks for empty fields */
//        if( emptyFields() ){
//            MessageDialog.showInfoDialog( (Frame)owner.getOwner(), owner.getTitle(), "Please fill in all particulars." );
//       
//        }else{
//            // check if any changes
//            if( !roleModified )
//                MessageDialog.showInfoDialog( (Frame)owner.getOwner(), owner.getTitle(), "No Changes Made!" );
//            // display last confirmation
//            else{
//                int ans;
//                String msg = "Member: " + tfNric.getText() + "\nChanges:";
//
//                if (nameModified)
//                    msg += "\nName: " + tfName.getText();
//                if (addModified)
//                    msg += "\nAddress: " + taAddress.getText();
//                if (contactModified)
//                    msg += "\nContact: " + tfContact.getText();
//                if (cbPlan.isSelected()) {
//                    msg += "\nUpgrading plan to PREMIUM";
//                }
//                msg += "\nContinue?";
//
//                ans = MessageDialog.showQuestionDialog((Frame) owner.getOwner(),
//                        owner.getTitle(), msg);
//
//                if (ans == MessageDialog.YES_OPTION) {
//                    /* do update by calling controller */
//                    if (cbPlan.isSelected()) {
//                        plan = cbPlan.getActionCommand();
//                    } else {
//                        plan = lCurrPlan.getText();
//                    }
//                    if (memberAdminSystem.updateMember(tfName.getText(),
//                            taAddress.getText(),
//                            tfContact.getText(),
//                            plan)) { //member successfully updated
//                        displayInfoDialog(1);
//                        clearFields();
//                    } else { // illegal changing of plan from premium to basic
//                        displayInfoDialog( -1);
//                        clearFields();
//                    }
//
//                    bUpdate.setEnabled(false);
//                    bView.setEnabled(true);
//                    bClear.setEnabled(false);
//                    tfNric.setEnabled(true);
//                    clearFields();
//                }
//            }
//        }
    }

   

    /* role field modified */
    public void tfUserName_keyTyped(KeyEvent e) {
        roleModified = true;
    }

}




class EditRemoveUserUI_tfUserName_keyAdapter extends KeyAdapter {
    private EditRemoveUserUI adaptee;
    EditRemoveUserUI_tfUserName_keyAdapter(EditRemoveUserUI adaptee) {
        this.adaptee = adaptee;
    }

    public void keyTyped(KeyEvent e) {
        adaptee.tfUserName_keyTyped(e);
    }
}


class EditRemoveUserUI_bRemove_actionAdapter implements ActionListener {
    private EditRemoveUserUI adaptee;
    EditRemoveUserUI_bRemove_actionAdapter(EditRemoveUserUI adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.bRemove_actionPerformed(e);
    }
}


class EditRemoveUserUI_bView_actionAdapter implements ActionListener {
    private EditRemoveUserUI adaptee;
    EditRemoveUserUI_bView_actionAdapter(EditRemoveUserUI adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.bView_actionPerformed(e);
    }
}


class EditRemoveUserUI_bEdit_actionAdapter implements ActionListener {
    private EditRemoveUserUI adaptee;
    EditRemoveUserUI_bEdit_actionAdapter(EditRemoveUserUI adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.bEdit_actionPerformed(e);
    }
}

class EditRemoveUserUI_bReset_actionAdapter implements ActionListener {
    private EditRemoveUserUI adaptee;
    EditRemoveUserUI_bReset_actionAdapter(EditRemoveUserUI adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.bReset_actionPerformed(e);
    }
}
